<?php

class Sendmail {
	//邮件服务器的SMTP
	private $smtpname   = '';
	
	//登录邮件服务器的用户名
	private $username   = '';
	
	//登录邮件服务器的密码
	private $password   = '';
	
	//发送方邮件地址
	private $myaddress  ='';
	
	//发送方姓名
	private $myname     = '';
	
	//邮件主题
	private $subject    = '';
	
	//邮件内容
	private $body       = '';
	
	//邮件附件
	private $attachment = '';
	
	//引用PHPMailer机制
	private $mail        = '';
	
	//构造发送方基本信息
	public function __construct($message,$plate) {
		$this->smtpname   = $message['smtpname'];
		$this->username   = $message['username'];
		$this->password   = $message['password'];
		$this->myaddress  = $message['myaddress'];
		$this->myname     = $message['myname'];
		
		$this->subject    = $plate['subject'];
		$this->body       = $plate['body'];
		$this->attachment = $plate['attachment'];
		
		//引用PHPMailer机制
		require_once 'phpmailer.class.php';
		$this->mail = new PHPMailer();
		
		$this->mail->IsSMTP(); // set mailer to use SMTP
		$this->mail->Host =$this->smtpname; // specify main and backup server
		$this->mail->SMTPAuth = true; // turn on SMTP authentication
		$this->mail->Username =$this->username; // SMTP username
		$this->mail->Password =$this->password; // SMTP password
		$this->mail->From =$this->myaddress;
		$this->mail->FromName =$this->myname;
		$this->mail->IsHTML(true); // set email format to HTML
		$this->mail->Subject = $this->subject;
		$this->mail->Body = $this->body;
		$this->mail->AltBody = "This is the body in plain text for non-HTML mail clients";
		
	    //如果附件存在就添加附件
		if (file_exists($this->attachment)) {
			$this->mail->AddAttachment($this->attachment); // add attachments
		}
	}
	
	//发送邮件
	public function send($address) {
		$this->mail->AddAddress($address);
		return $this->mail->Send();//返回发送信息
	}
	
	//显示内容--可删除
	public function xianshi() {
		echo $this->smtpname;
		echo "<br/>";
		echo $this->username;
		echo "<br/>";
		echo $this->password;
		echo "<br/>";
		echo $this->myaddress;
		echo "<br/>";
		echo $this->myname;
		echo "<br/>";
		
		echo $this->subject;
		echo "<br/>";
		echo $this->body;
		echo "<br/>";
		echo $this->attachment;
		echo "<br/>";
	}
	
	//解析函数
	public function __destruct() {
		$this->mail = '';
	}

}

?>